<?php

class Admin_PaymentController extends My_Controller_ActionAdmin {

    public function statsAction() {
        $daoPayment = new Application_Model_DAO_Payment();
        $dateStart = date(My_Db_Table::DATE_PATTERN, time() - 60*60*24*31);
        $this->view->dateStart = $dateStart = $this->_getParam('from', $dateStart);
        $dateEnd = date(My_Db_Table::DATE_PATTERN);
        $this->view->dateEnd = $dateEnd = $this->_getParam('to', $dateEnd);
        $stats = $daoPayment->getStats($dateStart, $dateEnd);
        $this->view->stats = $stats;
    }
    
    public function indexAction() {
        $daoPayment = new Application_Model_DAO_Payment();
        $select = $daoPayment->getAll();
        $this->view->list = $this->createPaginator($select);
    }

    public function saveAction() {
        $code = $this->_getParam('id');
        $isEdit = false;
        if (!empty($code)) {
            $daoPayment = new Application_Model_DAO_Payment();
            $record = $daoPayment->getByCode($code);
            $form = new Admin_Form_Payment_Edit();
            if (!$this->_request->isPost()) {
                $record['id'] = $code;
                $form->populate($record);
            }
            $isEdit = true;
        } else {
            $form = new Admin_Form_Payment_Edit();
        }
        if (!$this->_request->isPost() || !$form->isValid($_POST)) {
            $this->view->form = $form;
            $this->renderForm();
            return;
        }
        $data = $form->getValues();
        unset($data['id']);
        $daoPayment = new Application_Model_DAO_Payment();
        $comment = $data['comment'];
        $status = $data['status'];
        $logic = $form->getElement('perform_state_logic')->isChecked();
        if ($isEdit) {
            if($logic) {
                $record = $daoPayment->setPaymentStatus($record['payment_id'], $status, $comment, true);
            } else {
                $record = $daoPayment->setPaymentStatusRaw($record['payment_id'], $status, $comment);
            }
        } else {
//            $record = $daoPayment->addNew($data);
        }

        $this->addSuccess($this->translate('XXX_ADMIN_PAYMENT_EDIT_SUCCESS'));
        $url = $this->url(array('controller' => 'payment', 'action' => 'index'));
        $this->_redirect($url);
    }

}

